import java.io.*;
import java.util.regex.*;
import java.net.HttpURLConnection;
import java.net.URL;

class GetURL
{
    public static void report( String message ) 
    {
        System.out.println( "[Report] " + message );
    }

    public static void main(String argv[])
    {
        HttpURLConnection con = null;
        try {
            URL url = new URL("http://tw.yahoo.com");
            con = (HttpURLConnection) url.openConnection();            
            con.setReadTimeout(10000);
            con.setConnectTimeout(15000);
            con.setRequestMethod("GET" );
            con.addRequestProperty("User-Agent","Mozilla/5.0 (Windows; U; Windows NT 5.2; en-GB; rv:1.9.2.9) Gecko/20100824 Firefox/3.6.9");
            con.setDoInput(true);
            con.connect();

            BufferedReader reader = new BufferedReader(new InputStreamReader(con.getInputStream(), "UTF-8" ));
            String n, result="";
            //while( ( n = reader.readLine() ) != null )
            //  result += n;

            StringBuilder htmlContent = new StringBuilder();
            while ((n = reader.readLine()) != null)
               htmlContent.append(n);

            result = htmlContent.toString();

            int at;
            String pattern = "<label class=\"img-border clearfix\">";
            if((at = result.indexOf(pattern)) < 0 ) {
                report( "format error 1" );
                return;
            }
            result = result.substring(at);

            pattern = "<ol class=\"newsad clearfix\">";
            if((at = result.indexOf(pattern)) < 0 ) {
                report("format error 2");
                return;
            }
            result = result.substring( 0 , at  );

            pattern = "<h3[^>]*>[^<]*<a href=\"(.*?)\"[^>]*>(.*?)</a></h3>";
            Pattern p = Pattern.compile( pattern , Pattern.CASE_INSENSITIVE | Pattern.DOTALL );
            Matcher m = p.matcher( result );

            while(m.find()) {
                report( "\n==== Get === \n" + m.group() + "\n" );
                report( "URL: " + m.group(1) );
                report( "Title: " + m.group(2) );
            }
        }
        catch(Exception e) {
        }
        finally {
            if (con != null ) {
                con.disconnect();
            }
        }
    }
}
